package com.liangzhong.proxy.club
{
	import com.common.utils.Debug;
	import com.liangzhong.commandconst.club.SMDClub;
	import com.liangzhong.commandconst.commandType.MainCommandType;
	import com.liangzhong.commandconst.pub.CMD_Public;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.club.ClubDataManager;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.liangzhong.proxyconstant.activity.ActivityConst;
	import com.liangzhong.proxyconstant.club.ClubProxyConstant;
	import com.liangzhong.proxyconstant.compete.CompeteProxyConstant;
	import com.socket.clientsocket.IClientSocket;
	
	import flash.events.Event;
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;
	
	/**
	 *俱乐部proxy 
	 * @author nick
	 * 
	 */	
	public class ClubProxy extends BaseProxy
	{
		/**
		 *俱乐部proxy名字 
		 */
		public static const PROXYNAME:String = "proxyname";
		
		/**
		 * 
		 */
		private var _clubDataManager:ClubDataManager;
		
		public function ClubProxy(proxyName:String=PROXYNAME, data:Object=null)
		{
			super(proxyName, data);
			_clubDataManager = AppData.getInstance().clubDatamanager;
		}
		override public function onSocketRead(mainCmdID:uint, subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case CMD_Public.MAIN://公用协议
					return onDeal(subCmdID,buffer,dataSize,clientSocket);
				case MainCommandType.MAINDEAL:
					return onMainDeal(subCmdID,buffer,dataSize,clientSocket);
			}
			return false;
		}
		/**
		 * 处理
		 * 公共协议类型 40
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */
		private function onDeal(subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			var results:int;
			switch(subCmdID)
			{
				case CMD_Public.ECONOMY_VALUE_CHANGE:
					results = buffer.readInt();
					if(results == 0)
					{
						_clubDataManager.changeEconomy(buffer.readInt(),buffer.readInt(),buffer.readInt());
						this.sendNotification(ClubProxyConstant.ECONOMY_VALUE_CHANGE);
					}
					return true;
				case SMDClub.GET_NEW_SHIRT:
					var shirtId:int = buffer.readInt();
					_clubDataManager.trikotList.push(shirtId);
					return true;
			}
			return false;
		}
		/**
		 * 处理
		 * 主界面协议类型 10 
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */		
		private function onMainDeal(subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
//			if(_clubDataManager.clubInfo == null)return false;
			var results:int;
			switch(subCmdID)
			{
				case SMDClub.CHANGE_SHIRT:
					getChangeShirt(buffer);
					return true;
				case SMDClub.CHANGE_LOGO:
					getChangeLogo(buffer);
					return true;
				case SMDClub.CHANGE_NATION:
					getChangeNation(buffer);
					return true;
			}
			return false;
		}
		/**
		 *请求俱乐部信息 
		 * 
		 */
		public function requestClubInfo():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.CLUB_GETCLUBINFO;
			ProxyUtils.instance.webClient(getClubInfo,variables,false);
			requestPlayerInfo();
			requestBattlearrayInfo();
			requestLadderInfo();
			reqeusetTaskList();
			requestSysini();
			requestAchieveDate();
			requestOnLineAwardInfo();
			if(AppData.getInstance().competeDataManager.competeInfoVo == null || AppData.getInstance().competeDataManager.competeInfoVo.rivalcountry == 0)
				this.sendNotification(CompeteProxyConstant.COMPETE_MEDIATORTOCOMMAND,null,CompeteProxyConstant.COMPETEINFO);
		}
		/**
		 *获得俱乐部信息 
		 * @param $data
		 * 
		 */
		private function getClubInfo($data:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($data,ClubProxy);
			if(obj.retcode == 0)
			{
				_clubDataManager.analysisClubInfo(obj);
				this.sendNotification(ClubProxyConstant.GET_CLUB_INFO,_clubDataManager.clubInfo);
				if(AppData.getInstance().playerCenterDataManager.playerCenterVo != null)
					AppData.getInstance().playerCenterDataManager.playerCenterVo.updataphysicalTime = AppData.getInstance().clientHourse;
				AppEventDispatcher.getInstance().dispatchEvent(new Event(NotifierEvent.BASE_INFO_CHANGE));
				if(_clubDataManager.clubInfo.VIP > 0 && _clubDataManager.clubInfo.vip_reward_every ==0)
				{
					this.sendNotification(ActivityConst.LOGIN_VIP_DAILY_AWARD);
				}
			}else
			{
				Debug.echo("++++++++"+ErrorTips.instance.getErrorTip(obj.msg));
			}
			
			
		}
		/**
		 *更换队徽 
		 * @param $obj
		 * 
		 */
		public function changeLogo($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 2,MainCommandType.MAINDEAL,SMDClub.CHANGE_LOGO);
			var id:int = $obj.id;
			sendBuf.writeShort(id);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *g更换球衣 
		 * @param $obj
		 * 
		 */
		public function changeShirt($obj:Object):void
		{
			
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 4,MainCommandType.MAINDEAL,SMDClub.CHANGE_SHIRT);
			var id:int = $obj.id;
			sendBuf.writeInt(id);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回更换队徽 
		 * 
		 */
		private function getChangeLogo(buffer:ByteArray):void
		{
			
			
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
				var id:int = buffer.readShort();
				_clubDataManager.clubInfo.logo = id;
				this.sendNotification(ClubProxyConstant.GET_CHANGELOGO,{status:true});
				Alert.showErrorTip("更换成功");
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
				this.sendNotification(ClubProxyConstant.GET_CHANGELOGO,{status:false});
			}
		}
		/**
		 *返回更换球衣 
		 * 
		 */
		private function getChangeShirt(buffer:ByteArray):void
		{
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
				var id:int = buffer.readInt();
				_clubDataManager.clubInfo.homekot = id;
				this.sendNotification(ClubProxyConstant.GET_CHANGESHIRT,{status:true});
				Alert.showErrorTip("更换成功");
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
				this.sendNotification(ClubProxyConstant.GET_CHANGESHIRT,{status:false});
			}
		}
		/**
		 *更换国家 
		 * @param $obj
		 * 
		 */		
		public function changeNation($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 2,MainCommandType.MAINDEAL,SMDClub.CHANGE_NATION);
			var id:int = $obj.nationId;
			sendBuf.writeShort(id);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回更换国家 
		 * 
		 */
		private function getChangeNation(buffer:ByteArray):void
		{
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
				var id:int = buffer.readShort();
				_clubDataManager.clubInfo.changeNation(id);
				_clubDataManager.clubInfo.countrymodify = 1;
				this.sendNotification(ClubProxyConstant.GET_CHANGENATION,{status:true});
				if(AppData.getInstance().competeDataManager.competeInfoVo != null)
				{
					AppData.getInstance().competeDataManager.competeInfoVo.isupdateopponentinfo = true;
				}
						
				
				Alert.showErrorTip("更换成功");
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
				this.sendNotification(ClubProxyConstant.GET_CHANGENATION,{status:false});
			}
		}
		
		
		/**
		 *请求球员信息 
		 * 
		 */
		private function requestPlayerInfo():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.PLAYER_LIST_INFO;
			ProxyUtils.instance.webClient(getPlayerInfo,variables,false);
		}
		/**
		 *获得球员数据 
		 * @param $data
		 * 
		 */		
		private function getPlayerInfo($data:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($data,ClubProxy);
			if(obj.retcode == 0)
			{
//				_clubDataManager.analysisClubInfo(obj);
				AppData.getInstance().playerCenterDataManager.getPlayerList(obj);
			}else
			{
				Debug.echo("getPlayerInfo"+ErrorTips.instance.getErrorTip(obj.retcode));
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		/**
		 *请求阵容数据 
		 * 
		 */
		private function requestBattlearrayInfo():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.BATTLEARRAYINFO;
			ProxyUtils.instance.webClient(getBattlearrayInfo,variables,false);
		}
		/**
		 *返回阵容数据 
		 * @param $data
		 * 
		 */
		private function getBattlearrayInfo($data:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($data,ClubProxy);
			if(obj.retcode == 0)
			{
				AppData.getInstance().playerCenterDataManager.getBattlearrayInfo(obj);
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
				Debug.echo("getBattlearrayInfo"+ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		/**
		 *请求天梯信息 
		 * 
		 */
		private function requestLadderInfo():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.LADDER_GET_INFO;
			ProxyUtils.instance.webClient(getLadderInfo,variables,false);
		}
		/**
		 *返回天梯信息
		 * @param $data
		 * 
		 */
		private function getLadderInfo($data:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($data,ClubProxy);
			if(obj.retcode == 0)
			{
				AppData.getInstance().ladderDataManger.getLadderInfo(obj);
			}else
			{
				Debug.echo("getBattlearrayInfo"+ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		
		/**
		 *请求任务列表 
		 * 
		 */
		public function reqeusetTaskList():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.TASK_LIST;
			ProxyUtils.instance.webClient(getTaskList,variables,false);
		}
		/**
		 *返回获得任务列表 
		 * 
		 */
		private function getTaskList($data:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($data,ClubProxy);
			if(obj.retcode == 0)
			{
				var arr:Array = obj.task;
				AppData.getInstance().taskManager.getAcceptTaskList(arr);
			}else
			{
			}
		}
	
		/**
		 *请求活动数据 
		 * 
		 */		
		private function requestSysini():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.SYSINI;
			ProxyUtils.instance.webClient(getSysini,variables,false);
		}
		/**
		 *返回活动数据 
		 * @param $date
		 * 
		 */		
		private function getSysini($date:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($date,ClubProxy);
			if(obj.retcode == 0)
			{
				_clubDataManager.analysisSysini(obj.sysini);
			}
		}
		
		/**
		 *请求成就数据 
		 * 
		 */
		private function requestAchieveDate():void
		{
			
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.ACHIEVEDATE;
			ProxyUtils.instance.webClient(getAchieveDate,variables,false);
		}
		/**
		 *返回成就数据 
		 * @param $date
		 * 
		 */
		private function getAchieveDate($date:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($date,ClubProxy);
			if(obj.retcode == 0)
			{
				_clubDataManager.analysisAchieve(obj.achievedata);
			}
		}
		
		/**
		 *请求在线奖励信息 
		 * 
		 */
		private function requestOnLineAwardInfo():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.ONLICE_INFO;
			ProxyUtils.instance.webClient(getOnLineAwardInfo,variables,false);
		}
		/**
		 *返回在线奖励信息 
		 * 
		 */
		private function getOnLineAwardInfo($date:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($date,ClubProxy);
			if(obj.retcode == 0)
			{
//				_clubDataManager.analysisAchieve(obj.achievedata);
				AppData.getInstance().activityDatemanager.onLineInfo(obj.matchCount.status13,obj.matchCount.onlinetime);
				this.sendNotification(ActivityConst.GET_ONLINE_INFO);
			}
		}
			
	}
}